package hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


/**
 * Created by zhangbn on 2018/10/17.
 */
public class HBaseTest {

    private static Configuration conf = HBaseConfiguration.create();

    static {
        conf.set("hbase.rootdir", "hdfs://hsmaster:9000/hbase");
        // 设置Zookeeper,直接设置IP地址
        conf.set("hbase.zookeeper.quorum", "hsmaster,hsslave1,hsslave2");
    }

    // 创建表

    public static void createTable(String tablename, String columnFamily) throws Exception {

        Connection connection = ConnectionFactory.createConnection(conf);

        Admin admin = connection.getAdmin();


        TableName tableNameObj = TableName.valueOf(tablename);


        if (admin.tableExists(tableNameObj)) {

            System.out.println("Tableexists!");

            System.exit(0);

        } else {

            HTableDescriptor tableDesc = new HTableDescriptor(tableNameObj);

            tableDesc.addFamily(new HColumnDescriptor(columnFamily));

            admin.createTable(tableDesc);

            System.out.println("createtable success!");

        }

        admin.close();
        connection.close();
    }


// 删除表

    public static void deleteTable(String tableName) {

        try {

            Connection connection = ConnectionFactory.createConnection(conf);

            Admin admin = connection.getAdmin();

            TableName table = TableName.valueOf(tableName);

            admin.disableTable(table);

            admin.deleteTable(table);

            System.out.println("deletetable " + tableName + "ok.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


    // 插入一行记录

    public static void addRecord(String tableName, String rowKey, String family, String qualifier, String value) {

        try {

            Connection connection = ConnectionFactory.createConnection(conf);

            Table table = connection.getTable(TableName.valueOf(tableName));

            Put put = new Put(Bytes.toBytes(rowKey));

            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));

            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));

            table.put(put);

            table.close();

            connection.close();

            System.out.println("insertrecored " + rowKey + " totable " + tableName + "ok.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


    public static void main(String[] args) throws Exception {

        System.setProperty("hadoop.home.dir", "E:\\hadoop-common-2.7.3-bin-master");

        HBaseTest.createTable("bigdata:person-repo", "info");
//        HBaseTest.addRecord("t03", "u001", "info", "pass_id", "111");
//        HBaseTest.addRecord("t03", "u001", "info", "feature", "fff111");
//        HBaseTest.addRecord("t03", "u002", "info", "pass_id", "222");
//        HBaseTest.addRecord("t03", "u002", "info", "feature", "fff222");

//        HBaseTest.createTable("t02", "quality_score");
//        HBaseTest.addRecord("t02", "u001", "quality_score", "98", "p2");
//        HBaseTest.addRecord("t02", "u001", "quality_score", "68", "p5");

//        HBaseTest.deleteTable("testTb");

    }

}
